import 'package:flutter/cupertino.dart';
import 'package:petitparser/petitparser.dart';

import '../common/test_page.dart';

class ContextTestPage extends TestPage {
  ContextTestPage(String title, {Key? key}) : super(title, key: key) {
    const buffer = 'a\nc';
    const context = Context(buffer, 0);
    test('context', () {
      expect(context.buffer, buffer);
      expect(context.position, 0);
      expect(context.toString(), 'Context[1:1]');
    });
    group('success', () {
      test('default', () {
        final success = context.success('result');
        expect(success.buffer, buffer);
        expect(success.position, 0);
        expect(success.value, 'result');
        expect(success.isSuccess, null);
        expect(success.isFailure, null);
        expect(success.toString(), 'Success[1:1]: result');
      });
      test('default-1, 此处应该为X', () {
        final success = context.success('result');
        success.message;
      });
      test('with position', () {
        final success = context.success('result', 2);
        expect(success.buffer, buffer);
        expect(success.position, 2);
        expect(success.value, 'result');
        expect(success.isSuccess, null);
        expect(success.isFailure, null);
        expect(success.toString(), 'Success[2:1]: result');
      });
      test('with position-1, 此处应该为X', () {
        final success = context.success('result', 2);
        success.message;
      });
      test('with mapping', () {
        final success = context.success('result', 2).map((value) {
          expect(value, 'result');
          return 123;
        });
        expect(success.buffer, buffer);
        expect(success.position, 2);
        expect(success.value, 123);
        expect(success.isSuccess, null);
        expect(success.isFailure, null);
        expect(success.toString(), 'Success[2:1]: 123');
      });
      test('with mapping-1, 此处应该为X', () {
        final success = context.success('result', 2).map((value) {
          expect(value, 'result');
          return 123;
        });
        success.message;
      });
    });
    group('failure', () {
      test('default', () {
        final failure = context.failure('error');
        expect(failure.buffer, buffer);
        expect(failure.position, 0);
        expect(failure.message, 'error');
        expect(failure.isSuccess, null);
        expect(failure.isFailure, null);
        expect(failure.toString(), 'Failure[1:1]: error');
      });
      test('default-1, 此处应该为X', () {
        final failure = context.failure('error');
        failure.value;
      });

      test('with position', () {
        final failure = context.failure('error', 2);
        expect(failure.buffer, buffer);
        expect(failure.position, 2);
        expect(failure.message, 'error');
        expect(failure.isSuccess, null);
        expect(failure.isFailure, null);
        expect(failure.toString(), 'Failure[2:1]: error');
      });
      test('with position-1, 此处应该为X', () {
        final failure = context.failure('error', 2);
        failure.value;
      });

      test('with mapping', () {
        final failure = context.failure('error', 2).map((value) => fail('Not expected to be called'));
        expect(failure.buffer, buffer);
        expect(failure.position, 2);

        expect(failure.message, 'error');
        expect(failure.isSuccess, null);
        expect(failure.isFailure, null);
        expect(failure.toString(), 'Failure[2:1]: error');
      });

      test('with mapping-1, 此处应该为X', () {
        final failure = context.failure('error', 2).map((value) => fail('Not expected to be called'));

        failure.value;
      });
    });
  }
}
